package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.common.exception.ServiceException;
import com.zzx.entity.TProductAttribute;
import com.zzx.mapper.TProductAttributeMapper;
import com.zzx.service.TProductAttributeService;
import com.zzx.utils.EntityUtils;
import com.zzx.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author zzx
 * @description 针对表【t_product_attribute(产品属性表)】的数据库操作Service实现
 * @createDate 2025-06-20
 */
@Service
public class TProductAttributeServiceImpl extends ServiceImpl<TProductAttributeMapper, TProductAttribute>
        implements TProductAttributeService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductAttribute param) {
        LambdaQueryWrapper<TProductAttribute> wrapper = new LambdaQueryWrapper<>(TProductAttribute.class);
        // 多租户拦截器会自动添加 company_id 条件
        wrapper.eq(TProductAttribute::getAttributeName, param.getAttributeName());
        wrapper.eq(TProductAttribute::getAttributeValue, param.getAttributeValue());

        if (ObjectUtils.isEmpty(param.getProductAttributeId())) {
            param.setProductAttributeId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TProductAttribute::getProductAttributeId, param.getProductAttributeId());
            TProductAttribute dbEntity = this.getBaseMapper().selectById(param.getProductAttributeId());
            if (dbEntity != null) {
                EntityUtils.mergeNonNullProperties(dbEntity, param);
            }
        }

        List<TProductAttribute> existList = this.getBaseMapper().selectList(wrapper);
        if (!existList.isEmpty()) {
            throw new ServiceException("产品属性已存在");
        }

        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductAttribute param) {
        this.removeById(param.getProductAttributeId());
    }

    @Override
    public IPage<TProductAttribute> selectBy(TProductAttribute param) {
        LambdaQueryWrapper<TProductAttribute> wrapper = new LambdaQueryWrapper<>(TProductAttribute.class);

        if (!ObjectUtils.isEmpty(param.getAttributeName())) {
            wrapper.like(TProductAttribute::getAttributeName, param.getAttributeName());
        }
        if (!ObjectUtils.isEmpty(param.getAttributeValue())) {
            wrapper.like(TProductAttribute::getAttributeValue, param.getAttributeValue());
        }
        if (!ObjectUtils.isEmpty(param.getProductAttributeId())) {
            wrapper.eq(TProductAttribute::getProductAttributeId, param.getProductAttributeId());
        }

        // 多租户拦截器会自动添加 company_id 条件
        wrapper.orderByDesc(TProductAttribute::getCreateTime);

        IPage<TProductAttribute> tProductAttributeIPage = this.getBaseMapper().selectPage(
                PageUtils.createPage(param.getPageNum(), param.getPageSize()),
                wrapper
        );

        return tProductAttributeIPage;
    }

    @Override
    public TProductAttribute findById(String id) {
        return this.getBaseMapper().selectById(id);
    }
}
